﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CS_BEGINNER_TO_PROFICIENT.STD.ReflectionSTD
{
    public class DataSource
    { 
        public Dictionary<string, object> Data { get; set; }
    }

    public class TargetObject
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }

    public class ReflectionSTDP5
    {
        public static void Execute()
        {
            var dataSource = new DataSource
            {
                Data = new Dictionary<string, object>
                {
                    { "Name", "John Doe" },
                    { "Age", 30 }
                }
            };

            var targetObject = new TargetObject();
            var targetType = typeof(TargetObject);

            foreach (var kvp in dataSource.Data)
            {
                var property = targetType.GetProperty(kvp.Key);
                if (property != null && property.CanWrite)
                {
                    property.SetValue(targetObject, kvp.Value);
                }
            }
            Console.WriteLine($"Name: {targetObject.Name}, Age: {targetObject.Age}");
        }
    }
}